//
//  YFChatListCell.m
//  project
//
//  Created by yefei on 2019/4/23.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFChatListCell.h"
#import "HJSimpleMessage.h"
#import "HJChatTextMsgModel.h"

@interface YFChatListCell()
/**
 头像的圆环 View
 */
@property (nonatomic, strong) UIView *avatarCircleView;
/**
 头像 imageView
 */
@property (nonatomic, strong) UIImageView *avatarImageView;
/**
 用户名字 Label
 */
@property (nonatomic, strong) UILabel *nameLabel;
/**
 消息 summary Label
 */
@property (nonatomic, strong) UILabel *summarylabel;
/**
 时间 Label
 */
@property (nonatomic, strong) UILabel *timeLabel;
/**
 消息未读数 Label
 */
@property (nonatomic, strong) UILabel *unreadCountLabel;


@end

@implementation YFChatListCell

-(void)setModel:(RCConversationModel *)model {

    RCUserInfo *userInfo = model.extend;
    if (userInfo) {
        [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.portraitUri] placeholderImage:setImage(@"img_mydongtai_defualt_avatar")];
        self.nameLabel.text = userInfo.name;
    }else {
        self.avatarImageView.image = setImage(@"img_mydongtai_defualt_avatar");
        self.nameLabel.text = userInfo.name;
    }
    
    NSString *summary = @"";
    if ([model.lastestMessage isMemberOfClass:[RCImageMessage class]]) {
        summary = @"图片";
    }else if ([model.lastestMessage isMemberOfClass:[RCTextMessage class]]) {
        summary = [YFChatListLastMessageModel yy_modelWithJSON:model.lastestMessage.yy_modelToJSONString].content;
    }else if ([model.lastestMessage isMemberOfClass:[RCVoiceMessage class]]) {
        summary = @"语音";
    }else if ([model.lastestMessage isMemberOfClass:[RCFileMessage class]]) {
        summary = @"文件";
    }else if ([model.lastestMessage isMemberOfClass:[RCRichContentMessage class]]) {
        summary = @"图文";
    }else if ([model.lastestMessage isMemberOfClass:[RCLocationMessage class]]) {
        summary = @"位置";
    }else if ([model.lastestMessage isMemberOfClass:RCSightMessage.class]) {
        summary = @"视频";
    }else if ([model.lastestMessage isMemberOfClass: [HJSimpleMessage class]]) {
        HJSimpleMessage * msg = (HJSimpleMessage *)model.lastestMessage;
        HJSimpleModel * simpleModel = [HJSimpleModel yy_modelWithDictionary:msg.content];
        if (simpleModel.type.intValue==8) {// 邀请组队
            summary = @"邀约同行";
        }else if (simpleModel.type.intValue==9) {// 修改行程组队
            summary = @"修改行程";
        }else if (simpleModel.type.intValue==10) {//拒绝行程组队
            summary = @"拒绝同行";
        }else if (simpleModel.type.intValue==11) {//同意行程组队
            summary = @"同意同行";
        }
    }else {
        summary = @"自定义";
    }
    self.summarylabel.text = summary;
    
    long long time = model.lastestMessageDirection == MessageDirection_SEND ? model.sentTime : model.receivedTime;
    
    self.timeLabel.text = [NSString strTimeWithTimestamp:time];
    
    self.unreadCountLabel.hidden = model.unreadMessageCount == 0;
    self.unreadCountLabel.text = [NSString stringWithFormat:@"%zd", model.unreadMessageCount];
    
    [self layoutIfNeeded];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupViews];
    }
    
    return self;
}

- (void)setupViews {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    // 头像
    [self.contentView addSubview:self.avatarCircleView];
    [self.avatarCircleView addSubview:self.avatarImageView];
    // 名字
    [self.contentView addSubview:self.nameLabel];
    // 消息摘要
    [self.contentView addSubview:self.summarylabel];
    // 时间
    [self.contentView addSubview:self.timeLabel];
    // 消息未读条数
    [self.contentView addSubview:self.unreadCountLabel];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    // 头像圆环约束
    @weakify(self)
    [self.avatarCircleView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).with.offset(10);
    }];
    
    // 头像约束
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.edges.equalTo(self.avatarCircleView).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];

    // 消息未读条数约束
    [self.unreadCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.right.equalTo(self.contentView).with.inset(10);
        make.bottom.equalTo(self.contentView).with.inset(10);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];

    // 时间约束
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(self.contentView).with.inset(10);
        make.right.equalTo(self.contentView).with.inset(10);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];

    // 名字约束
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(self.contentView).with.offset(7);
        //make.top.equalTo(self.avatarImageView);
        make.left.equalTo(self.avatarCircleView.mas_right).with.offset(8);
        make.height.mas_equalTo(30);
        make.right.greaterThanOrEqualTo(self.timeLabel.mas_left).with.inset(10);
    }];

    // 消息摘要约束
    [self.summarylabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.bottom.equalTo(self.avatarCircleView.mas_bottom).with.inset(0);
        make.left.equalTo(self.avatarCircleView.mas_right).with.offset(8);
        make.height.mas_equalTo(15);
        make.right.equalTo(self.timeLabel.mas_right).with.inset(25);
        //make.right.greaterThanOrEqualTo(self.unreadCountLabel.mas_left).with.inset(10);
    }];
    
    // 设置样式
    [self.avatarCircleView rounded:(UIRectCornerAllCorners) radius:20 borderColor:[UIColor blackColor] borderWidth:1];
    [self.avatarImageView rounded:(UIRectCornerAllCorners) radius:self.avatarImageView.height * 0.5];
    [self.unreadCountLabel rounded:(UIRectCornerAllCorners) radius:7.5];
}



#pragma mark - Lazy Load
- (UIView *)avatarCircleView {
    if (!_avatarCircleView) {
        _avatarCircleView = [UIView new];
        _avatarCircleView.backgroundColor = [UIColor whiteColor];
        _avatarImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _avatarCircleView;
}

- (UIImageView *)avatarImageView {
    if (!_avatarImageView) {
        _avatarImageView = [UIImageView new];
    }
    return _avatarImageView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont fontWithName:PingFangSC_Medium size:15];
        _nameLabel.textColor = [UIColor withRGB:51];
    }
    return _nameLabel;
}

- (UILabel *)summarylabel {
    if (!_summarylabel) {
        _summarylabel = [UILabel new];
        _summarylabel.font = [UIFont fontWithName:PingFangSC_Medium size:13];
        _summarylabel.textColor = [UIColor withRGB:153];
    }
    return _summarylabel;
}

-(UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.font = [UIFont fontWithName:PingFangSC_Medium size:12];
        _timeLabel.textColor = [UIColor withRGB:153];
        _timeLabel.textAlignment = NSTextAlignmentRight;
    }
    return _timeLabel;
}

- (UILabel *)unreadCountLabel {
    if (!_unreadCountLabel) {
        _unreadCountLabel = [UILabel new];
        _unreadCountLabel.backgroundColor = [UIColor withRed:255 green:75 blue:100];
        _unreadCountLabel.font = [UIFont fontWithName:PingFangSC_Medium size:10];
        _unreadCountLabel.hidden = YES;
        _unreadCountLabel.textColor = [UIColor whiteColor];
        _unreadCountLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _unreadCountLabel;
}

@end


@implementation YFChatListLastMessageModel



@end
